home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zcsdevn.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  9.0 KB  |  289 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zcsdevn.c,v 1.3 2000/09/19 19:00:53 lpd Exp $ */
  20. /* DeviceN color space support */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gxcspace.h"        /* must precede gscolor2.h */
  25. #include "gscolor2.h"
  26. #include "gscdevn.h"
  27. #include "gxcdevn.h"
  28. #include "estack.h"
  29. #include "ialloc.h"
  30. #include "icremap.h"
  31. #include "ifunc.h"
  32. #include "igstate.h"
  33. #include "iname.h"
  34. #include "ostack.h"
  35. #include "store.h"
  36.  
  37. /* Imported from gscdevn.c */
  38. extern const gs_color_space_type gs_color_space_type_DeviceN;
  39.  
  40. /* Forward references */
  41. private int ztransform_DeviceN(P5(const gs_device_n_params * params,
  42.                   const float *in, float *out,
  43.                   const gs_imager_state *pis, void *data));
  44.  
  45. /* <array> .setdevicenspace - */
  46. /* The current color space is the alternate space for the DeviceN space. */
  47. private int
  48. zsetdevicenspace(i_ctx_t *i_ctx_p)
  49. {
  50.     os_ptr op = osp;
  51.     const ref *pcsa;
  52.     gs_separation_name *names;
  53.     gs_device_n_map *pmap;
  54.     uint num_components;
  55.     gs_color_space cs;
  56.     ref_colorspace cspace_old;
  57.     gs_function_t *pfn;
  58.     int code;
  59.  
  60.     check_read_type(*op, t_array);
  61.     if (r_size(op) != 4)
  62.     return_error(e_rangecheck);
  63.     pcsa = op->value.const_refs + 1;
  64.     if (!r_is_array(pcsa))
  65.     return_error(e_typecheck);
  66.     num_components = r_size(pcsa);
  67.     if (num_components == 0)
  68.     return_error(e_rangecheck);
  69.     if (num_components > GS_CLIENT_COLOR_MAX_COMPONENTS)
  70.     return_error(e_limitcheck);
  71.     check_proc(pcsa[2]);
  72.     cs = *gs_currentcolorspace(igs);
  73.     if (!cs.type->can_be_alt_space)
  74.     return_error(e_rangecheck);
  75.     code = alloc_device_n_map(&pmap, imemory, ".setdevicenspace(map)");
  76.     if (code < 0)
  77.     return code;
  78.     names = (gs_separation_name *)
  79.     ialloc_byte_array(num_components, sizeof(gs_separation_name),
  80.               ".setdevicenspace(names)");
  81.     if (names == 0) {
  82.     ifree_object(pmap, ".setdevicenspace(map)");
  83.     return_error(e_VMerror);
  84.     }
  85.     {
  86.     uint i;
  87.     ref sname;
  88.  
  89.     for (i = 0; i < num_components; ++i) {
  90.         array_get(pcsa, (long)i, &sname);
  91.         switch (r_type(&sname)) {
  92.         case t_string:
  93.             code = name_from_string(&sname, &sname);
  94.             if (code < 0) {
  95.             ifree_object(names, ".setdevicenspace(names)");
  96.             ifree_object(pmap, ".setdevicenspace(map)");
  97.             return code;
  98.             }
  99.             /* falls through */
  100.         case t_name:
  101.             names[i] = name_index(&sname);
  102.             break;
  103.         default:
  104.             ifree_object(names, ".setdevicenspace(names)");
  105.             ifree_object(pmap, ".setdevicenspace(map)");
  106.             return_error(e_typecheck);
  107.         }
  108.     }
  109.     }
  110.     /* See zcsindex.c for why we use memmove here. */
  111.     memmove(&cs.params.device_n.alt_space, &cs,
  112.         sizeof(cs.params.device_n.alt_space));
  113.     gs_cspace_init(&cs, &gs_color_space_type_DeviceN, NULL);
  114.     cspace_old = istate->colorspace;
  115.     istate->colorspace.procs.special.device_n.layer_names = pcsa[0];
  116.     istate->colorspace.procs.special.device_n.tint_transform = pcsa[2];
  117.     cs.params.device_n.names = names;
  118.     cs.params.device_n.num_components = num_components;
  119.     cs.params.device_n.map = pmap;
  120.     pfn = ref_function(pcsa + 2);
  121.     if (pfn)
  122.     gs_cspace_set_devn_function(&cs, pfn);
  123.     else
  124.     pmap->tint_transform = ztransform_DeviceN;
  125.     code = gs_setcolorspace(igs, &cs);
  126.     if (code < 0) {
  127.     istate->colorspace = cspace_old;
  128.     ifree_object(names, ".setdevicenspace(names)");
  129.     ifree_object(pmap, ".setdevicenspace(map)");
  130.     return code;
  131.     }
  132.     rc_decrement(pmap, ".setdevicenspace(map)");  /* build sets rc = 1 */
  133.     pop(1);
  134.     return 0;
  135. }
  136.  
  137. /* ------ Internal procedures ------ */
  138.  
  139. /* Forward references */
  140. private int devicen_remap_transform(P5(const gs_device_n_params * params,
  141.                        const float *in, float *out,
  142.                        const gs_imager_state *pis,
  143.                        void *data));
  144. private int devicen_remap_prepare(P1(i_ctx_t *));
  145. private int devicen_remap_finish(P1(i_ctx_t *));
  146. private int devicen_remap_cleanup(P1(i_ctx_t *));
  147.  
  148. /* Map to a concrete color by calling the tint_transform procedure. */
  149. private int
  150. ztransform_DeviceN(const gs_device_n_params * params, const float *in,
  151.            float *out, const gs_imager_state *pis, void *data)
  152. {
  153.     /* Just schedule a call on the real tint_transform. */
  154.     int_remap_color_info_t *prci =
  155.     r_ptr(&gs_int_gstate((const gs_state *)pis)->remap_color_info,
  156.           int_remap_color_info_t);
  157.  
  158.     prci->proc = devicen_remap_prepare;
  159.     memcpy(prci->tint, in, params->num_components * sizeof(float));
  160.     return_error(e_RemapColor);
  161. }
  162.  
  163. /* Prepare to run tint_transform. */
  164. private int
  165. devicen_remap_prepare(i_ctx_t *i_ctx_p)
  166. {
  167.     gs_state *pgs = igs;
  168.     const gs_color_space *pcs = gs_currentcolorspace(pgs);
  169.     const gs_color_space *pbcs;
  170.     gs_client_color cc;
  171.     frac ignore_conc[GX_DEVICE_COLOR_MAX_COMPONENTS];
  172.  
  173.     /*
  174.      * Find the DeviceN space.  The worst case is an uncolored Pattern over
  175.      * an Indexed space over a DeviceN space.
  176.      */
  177.     if (gs_color_space_get_index(pcs) == gs_color_space_index_Pattern) {
  178.     pcs = gs_cspace_base_space(pcs);
  179.     }
  180.     pbcs = pcs;
  181.     if (gs_color_space_get_index(pbcs) != gs_color_space_index_DeviceN) {
  182.     pbcs = gs_cspace_base_space(pbcs);
  183.     }
  184.     /*
  185.      * Temporarily set the tint_transform procedure in the color space to
  186.      * the one that calls the PostScript code, and its data to the context
  187.      * pointer.  This is a hack, but a localized one, to get an Indexed
  188.      * color space to do the table lookup.
  189.      */
  190.     memcpy(cc.paint.values,
  191.        r_ptr(&istate->remap_color_info, int_remap_color_info_t)->tint,
  192.        pbcs->params.device_n.num_components * sizeof(float));
  193.     pbcs->params.device_n.map->tint_transform = devicen_remap_transform;
  194.     pbcs->params.device_n.map->tint_transform_data = i_ctx_p;
  195.     return pcs->type->concretize_color(&cc, pcs, ignore_conc,
  196.                        (gs_imager_state *)pgs);
  197. }
  198.  
  199. /* Run the tint_transform procedure on the tint values. */
  200. private int
  201. devicen_remap_transform(const gs_device_n_params * params, const float *in,
  202.             float *out, const gs_imager_state *pis, void *data)
  203. {
  204.     i_ctx_t *i_ctx_p = data;
  205.     int num_in = params->num_components;
  206.     int num_out =
  207.     gs_color_space_num_components((const gs_color_space *)
  208.                       ¶ms->alt_space);
  209.     int i;
  210.  
  211.     check_estack(num_in + 4);
  212.     check_ostack(num_in);
  213.     for (i = 0; i < num_in; ++i) {
  214.     ++osp;
  215.     make_real(osp, in[i]);
  216.     *++esp = *osp;
  217.     }
  218.     ++esp;
  219.     make_int(esp, num_in);
  220.     push_mark_estack(es_other, devicen_remap_cleanup);
  221.     push_op_estack(devicen_remap_finish);
  222.     *++esp = istate->colorspace.procs.special.device_n.tint_transform;
  223.     /*
  224.      * Initialize the output values to nominal legal ones so that the
  225.      * concretize_color procedure doesn't get arithmetic exceptions.
  226.      */
  227.     for (i = 0; i < num_out; ++i)
  228.     out[i] = 0;
  229.     /* Restore the tint_transform in the color space. */
  230.     params->map->tint_transform = ztransform_DeviceN;
  231.     params->map->tint_transform_data = 0;
  232.     return o_push_estack;
  233. }
  234.  
  235. /* Save the transformed color value. */
  236. private int
  237. devicen_remap_finish(i_ctx_t *i_ctx_p)
  238. {
  239.     gs_state *pgs = igs;
  240.     const gs_color_space *pcs = gs_currentcolorspace(pgs);
  241.     const gs_device_n_params *params;
  242.     const gs_color_space *pacs;
  243.     gs_device_n_map *map;
  244.     int num_in, num_out;
  245.     float conc[GX_DEVICE_COLOR_MAX_COMPONENTS];
  246.     int code;
  247.     int i;
  248.  
  249.     while (gs_color_space_get_index(pcs) != gs_color_space_index_DeviceN) {
  250.     pcs = gs_cspace_base_space(pcs);
  251.     }
  252.     params = &pcs->params.device_n;
  253.     num_in = params->num_components; /* also on e-stack */
  254.     pacs = (const gs_color_space *)¶ms->alt_space;
  255.     map = params->map;
  256.     num_out = gs_color_space_num_components(pacs);
  257.     code = float_params(osp, num_out, conc);
  258.     if (code < 0)
  259.     return code;
  260.     esp -= num_in + 2;        /* mark, count, tint values */
  261.     for (i = 0; i < num_in; ++i)
  262.     map->tint[i] = esp[i + 1].value.realval;
  263.     for (i = 0; i < num_out; ++i)
  264.     map->conc[i] = float2frac(conc[i]);
  265.     map->cache_valid = true;
  266.     osp -= num_out;
  267.     return o_pop_estack;
  268. }
  269.  
  270. /* Clean up by removing the tint values from the e-stack. */
  271. private int
  272. devicen_remap_cleanup(i_ctx_t *i_ctx_p)
  273. {
  274.     int num_in = esp->value.intval;
  275.  
  276.     esp -= num_in + 1;
  277.     return o_pop_estack;
  278. }
  279.  
  280. /* ------ Initialization procedure ------ */
  281.  
  282. const op_def zcsdevn_op_defs[] =
  283. {
  284.     op_def_begin_ll3(),
  285.     {"1.setdevicenspace", zsetdevicenspace},
  286.     {"0%devicen_remap_prepare", devicen_remap_prepare},
  287.     op_def_end(0)
  288. };
  289.